package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.dynamicform.util.DynamicFormBusUtil;

public class ImgUploadDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return "";
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap);
		String src = MapUtil.getStringValue(formMap, formColumnDomain.getName());		
		String visiable = "";
		if(StringUtil.isEmpty(src))
			visiable = "visibility:hidden;";
		if(!StringUtil.isEmpty(src))
			src = "src=\"" + src + "\"";
		String showclass = " width: 120px; height: 130px;";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			showclass = formColumnDomain.getShowclass();
		String jscontrol = " onload=\"if(this.width>100) this.width=100;if(this.height>120) this.height=120;\"";
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			jscontrol = formColumnDomain.getJs();
		StringBuffer str = new StringBuffer();
		str.append("<div id=\"div_photo\"").append(" style=\"").append(visiable).append(showclass).append("overflow: hidden\">")
			.append("<img  id=\"img\" ").append(src).append(" class=\"photo\" ").append(jscontrol).append("/>");
		str.append("<div id=\"forIE7\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);\"></div>")
			.append("</div><div style=\"width: 55px; height: 20px; border: none; padding: 1px 2px 1px 0px; \" title=\"选择图片\">")
			.append("<input type=\"file\" name=\"upload\" id=\"fileId\" style=\"width: 70px; border: none; margin: -1px;\" onchange=\"showPhoto(this,'div_photo','img', 'gif,jpg,jpeg,png,bmp');\"/>")
			//.append("<input type=\"hidden\" name=\"photo__name\" id=\"photo__name\"/>")
			.append("</div>").append("<input type=\"hidden\" name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName())
			.append("\" value=\"").append(MapUtil.getStringValue(formMap, formColumnDomain.getName())).append("\"/>");
		if(!StringUtil.isEmpty(src))
			str.append("<img src=\"images/btn_clear.gif\" alt=\"清空\" width=\"22\" height=\"22\" style=\"cursor: hand;\" onClick=\"clearImgFile('img', '").append(formColumnDomain.getName()).append("');\" border=\"0\" align=\"absmiddle\">");
		
		if(!StringUtil.isEmpty(formColumnDomain.getNotemsg()))
			str.append(formColumnDomain.getNotemsg());
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String src = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		StringBuffer str = new StringBuffer();
		String showclass = " width: 120px; height: 130px;";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			showclass = formColumnDomain.getShowclass();
		String jscontrol = " onload=\"if(this.width>100) this.width=100;if(this.height>120) this.height=120;\"";
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			jscontrol = formColumnDomain.getJs();

		str.append("<div id=\"div_photo\" ").append(" style=\"").append(showclass).append(" overflow: hidden\">");
		if(!StringUtil.isEmpty(src))
			str.append("<img  id=\"img\" src=\"").append(src).append("\" class=\"photo\" ")
				.append(jscontrol).append("/>");		
		str.append("</div>");
		
		return str.toString();
	}
	
	public void dealFormValue(FormColumnDomain formColumnDomain, HashMap formMap) {
		String typecode = formColumnDomain.getTypecode();
		String curFile = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		if(!MapUtil.isEmpty(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH))
			curFile = MapUtil.getStringValue(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH);
		if(StringUtil.isEmpty(typecode)) {
			formMap.put("imgsrc", curFile);
			formMap.put("photosrc", curFile);
		} else 
			formMap.put(typecode, curFile);
	}
}